/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-08 10:05:40
 */
import { Body, Controller, Get, HttpCode, Post, Query, UseGuards, Headers, Param } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
import { ApiBody, ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { throwHttp } from 'src/utlis';
import { isTrue } from 'src/utlis/tools';
import { GetUserPage, UpdatePassWordDto, UpdateUserInfoDto } from './dto/user.dto';
import { UserEntity, UserResult } from './entities/user.entity';
import { UserService } from './user.service';
import { TokenDto } from 'src/common/auth/constants';
import { IdDto } from 'src/common/dto/dto';
@ApiTags('用户')
@Controller('admin/user')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly jwtService: JwtService,
  ) { }
  /** 用户列表 */
  @HttpCode(200)
  @Get('/getUserPage')
  @ApiResponse({
    status: 200,
    description: '用户列表',
    type: UserResult,
  })
  @ApiOperation({
    tags: ['获取用户列表'],
    summary: '获取用户列表',
    operationId: 'getUserPage',
  })
  @UseGuards(AuthGuard('jwt'))
  getUserPage(@Query() params: GetUserPage) {
    return this.userService.getUserPage(params);
  }

  /** 用户详情 */
  @HttpCode(200)
  @Get('/adminGetUserInfo')
  @ApiResponse({
    status: 200,
    description: '用户详情',
    type: UserEntity,
  })
  @ApiOperation({
    tags: ['获取用户详情'],
    summary: '获取用户详情',
    operationId: 'adminGetUserInfo',
  })
  @UseGuards(AuthGuard('jwt'))
  async adminGetUserInfo(@Query() { id }: IdDto) {
    return await this.userService.getUserInfo(id);
  }
  /** 用户详情 */
  // @HttpCode(200)
  // @Get('/getUserInfo')
  // @ApiResponse({
  //   status: 200,
  //   description: '用户详情',
  //   type: UserEntity,
  // })
  // @ApiOperation({
  //   tags: ['获取用户详情'],
  //   summary: '获取用户详情',
  //   operationId: 'getUserInfo',
  // })
  // @UseGuards(AuthGuard('userJwt'))
  // async getUserInfo(@Headers('authorization') authorization) {
  //   if (!isTrue(authorization)) {
  //     throwHttp('获取token失败');
  //   }
  //   const token = authorization.split(' ');
  //   const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
  //   const data = await this.userService.getUserInfo(user.userId);
  //   return data;
  // }
  /** 修改密码 */
  @HttpCode(200)
  @Post('/updatePassWord')
  @ApiBody({ description: '修改密码', type: UpdatePassWordDto })
  @ApiOperation({ summary: '修改密码', operationId: 'updatePassWord' })
  @UseGuards(AuthGuard('userJwt'))
  @ApiResponse({ status: 200, type: String })
  updatePassWord(@Body() body: UpdatePassWordDto, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    return this.userService.updatePassWord(body, user.userId);
  }
  /** 修改用户信息 */
  @HttpCode(200)
  @Post('/updateUserInfo')
  @ApiBody({ description: '修改用户信息', type: UpdateUserInfoDto })
  @ApiOperation({ summary: '修改用户信息', operationId: 'updateUserInfo' })
  @UseGuards(AuthGuard('userJwt'))
  @ApiResponse({ status: 200, type: String })
  updateUserInfo(@Body() body: UpdateUserInfoDto, @Headers('authorization') authorization) {
    if (!isTrue(authorization)) {
      throwHttp('获取token失败');
    }
    const token = authorization.split(' ');
    const user: TokenDto = this.jwtService.decode(token[1]) as TokenDto;
    return this.userService.updateUserInfo(body, user.userId);
  }
}
